package com.rtz.util;


import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class OptUtils {
	
	public SuccResult getSuccResult(List<?> values) {
		return new SuccResult(values);
	}
	
	public SuccResult getSuccResult(Map<String, Object> values) {
		return new SuccResult(values);
	}
	
	public SuccResult getSuccResult(Object value) {
		return new SuccResult(value);
	}
	
	public SuccResult getSuccResult(String value) {
		return new SuccResult(value);
	}
	
	public MessageResult getErrorResult(Integer code, Exception e) {
		return new ErrorResult(code, e);
	}
	
	public Exception getOptException(String msg) {
		return new Exception(msg);
	}
	
	/**
	 * 非空校验
	 * @param list
	 * @return
	 */
	public final boolean checkLists(List<?> list) {
		if(list == null) {
			return false;
		}else {
			return true;
		}
	}
	
	
	/**
	 * 返回值
	 * @param values List<?>
	 * @return
	 */
	public final Result SELECT_RESULT(List<?> values) {
		return getSuccResult(values);
	}
	
	/**
	 * 
	 * @param value Object
	 * @return
	 */
	public final Result SELECT_RESULT(Object value) {
		return getSuccResult(value);
	}
	
	
	/**
	 * 保存总数大于自己设置的总数
	 * @param insertCount
	 * @param logicCount
	 * @return
	 */
	public final Result INSERT_RESULT(final int insertCount, final int logicCount) {
		if (insertCount >= logicCount) {
			return getSuccResult(Operator.INSERT_SUCCESS);
		} else {
			return getErrorResult(500, getOptException(Operator.INSERT_FAILED));
		}
	}
	
	/**
	 * 保存总数大于1
	 * @param insertCount
	 * @param logicCount
	 * @return
	 */
	public final Result INSERT_RESULT(final int insertCount) {
		if (insertCount >= 1) {
			return getSuccResult(Operator.INSERT_SUCCESS);
		} else {
			return getErrorResult(500, getOptException(Operator.INSERT_FAILED));
		}
	}
	
	/**
	 * 更新总数大于等于1
	 * @param updateCount 更新数
	 * @return
	 */
	public final Result UPDATE_RESULT(final int updateCount) {
		if (updateCount >= 1) {
			return getSuccResult(Operator.UPDATE_SUCCESS);
		} else {
			return getErrorResult(500, getOptException(Operator.UPDATE_FAILED));
		}
	}
	
	/**
	 * 删除总数大于等于1
	 * @param deleteCount 删除数
	 * @return
	 */
	public final Result DELETE_RESULT(final int deleteCount) {
		if (deleteCount >= 1) {
			return getSuccResult(Operator.UPDATE_SUCCESS);
		} else {
			return getErrorResult(500, getOptException(Operator.UPDATE_FAILED));
		}
	}
	
	/**
	 * 逻辑操作
	 * @param opt_back 操作返回值
	 * @return
	 */
	public final Result OPT_RESULT(final boolean opt_back) {
		if (opt_back) {
			return getSuccResult(Operator.UPDATE_SUCCESS);
		} else {
			return getErrorResult(500, getOptException(Operator.UPDATE_FAILED));
		}
	}
	
	
	public final Result ERROR_RESULT(final Integer code,final Exception exp) {
		return getErrorResult(code, exp);
	}
	
	
	
}
